<template>
  <div class="app-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>删除数据库</span>
      </div>

      <el-button type="primary" @click="clearDatabaseConfirm">删除</el-button>
      <!--<div v-for="o in 4" :key="o" class="text item">-->
      <!--{{'列表内容 ' + o }}-->
      <!--</div>-->
      <p class="timingsMsg">注意：此操作会清空数据库内的全部数据</p>
    </el-card>
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      :modal-append-to-body='false'
      width="30%">
      <span>确定要删除数据库吗？</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="clearDatabase">确 定</el-button>
     </span>
    </el-dialog>
    <el-dialog
      title="提示"
      :visible.sync="dialogVisibleRestart"
      :modal-append-to-body='false'
      width="30%">
      <span>删除成功，重启后生效！</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisibleRestart = false">稍后重启</el-button>
        <el-button type="primary" @click="restart">立即重启</el-button>
     </span>
    </el-dialog>
  </div>
</template>

<script>
    export default {
        name: "clearDatabase",
      data:function(){
        return{
          dialogVisible: false,
          loading:false,
          restartStatus:false,
          dialogVisibleRestart:false
        }
      },
      methods:{
        clearDatabaseConfirm:function () {
          var _this=this
            _this.dialogVisible=true

        },
        clearDatabase:function () {
          console.log('清空数据库');
          var _this=this
          _this.getAjax({
            params:{
              opt:'delete_db',
              ajaxTime:new Date().getTime()
            }
          }).then(function (res) {
            _this.dialogVisible=false
            _this.dialogVisibleRestart=true
          }).catch(function (err) {
            console.log(err);
          }).finally(function (fn) {
            console.log(fn);
          })

        },
        restart:function () {
          var _this=this
          _this.dialogVisibleRestart=false

          // _this.loading=true

          console.log('确定重启');


          _this.getAjax({
            params:{
              opt:'device-reboot',
              ajaxTime:new Date().getTime()
            }
          }).then(function (res) {
            console.log('重启回调参数',res);
            var loading = _this.$loading({
              target:'.app-main',
              lock: true,
              text: '正在重启，请稍后...',
              spinner: 'el-icon-loading',
              background: 'rgba(0, 0, 0, 0.7)'
            });
            var restartInterval=setInterval(function () {
              if(_this.restartStatus){
                _this.tips('重启成功','success')
                console.log('重启成功');
                clearInterval(restartInterval)
                loading.close()
                // _this.tips('重启成功','success')
                setTimeout(function () {
                  _this.$router.push({ path: '/' })
                },1000)
              }else{
                _this.getversion()
              }

            },2000)
            // setTimeout(() => {
            //   loading.close();
            // }, 2000);
          }).catch(function (err) {
            console.log(err);
          }).finally(function (fn) {
            console.log(fn);
          })

          return

          var restartInterval=setInterval(function () {
            if(_this.restartStatus){
              console.log('重启成功');
              clearInterval(restartInterval)
              loading.close();
              this.$alert('3秒后自动跳转到登录页面', '重启成功');
              // _this.tips('重启成功','success')
              setTimeout(function () {
                this.$router.push({ path: '/' })
              },3000)
            }else{
              _this.getversion()
            }

          },2000)
        },
        // 获取版本信息
        getversion:function(restartStatus){
          var _this=this
          _this.postAjax({
            method:'getversion',
            params:{}
          }).then(function (res) {
            console.log(res);
            if(res.result==0){
              setTimeout(function () {
                _this.restartStatus=true
              },2000)
            }else{
              // _this.tips(res.errorInfo.detail,'error')
            }
          }).catch(function (err) {
            console.log(err);
          }).finally(function (fn) {
            console.log(fn);
          })
        }
      }
    }
</script>

<style scoped>

</style>
